@extends('admin.base')

@section('content')
    <div class="layui-card">
        <!--<div class="layui-card-header layuiadmin-card-header-auto">
            <h5>绑定设备</h5>
        </div>
        -->
        <div class="layui-card-body">
            <form lay-filter="create-article" class="layui-form" action="{{route('admin.site.binddevice')}}" method="post" >
            
                {{csrf_field()}}
                <input type="hidden" name="site_id" id="site_id" value="{{$site['id']??0}}">
                <input type="hidden" name="renter_id" id="renter_id" value="{{$site['renter_id']??0}}">
                <input type="hidden" name="device_id" id="device_id" value="{{$site->device->id??0}}">
                <input type="hidden" name="mode_ids_str" id="mode_ids_str" value="{{$mode_ids_str??""}}">
                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                    <legend>关联机器</legend>
                </fieldset>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">机器编号</label>
                        <div class="layui-input-inline" >
                            <input type="text" name="device_no" id="device_no" value="{{$site['device']['device_no']??old('device_no')}}" class="layui-input" lay-verify="device_no" />
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">机器厂家</label>
                        <div class="layui-input-inline" >
                            <input type="hidden" name="factory_id" id="factory_id" value="{{$site['device']['factory']['id']??old('id')}}" class="layui-input" lay-verify="factory_id"/>
                            <input type="text" name="factory_name" id="factory_name" value="{{$site['device']['factory']['name']??old('name')}}" class="layui-input" lay-verify="required"/>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">机器型号</label>
                        <div class="layui-input-inline" >
                            <input type="text" name="device_model" id="device_model" value="{{$site['device']['device_model']??old('device_model')}}" class="layui-input" lay-verify="device_model"/>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">安装时间</label>
                        <div class="layui-input-inline" >
                            <input type="text" name="install_time" id="install_time" value="{{$site['install_time']??old('install_time')}}" class="layui-input" lay-verify="install_time"/>
                        </div>
                    </div>
                </div>
                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                    <legend>服务配置</legend>
                </fieldset>

                <div id="xichemoshi">
                @if (!empty($factory_washmode))
                @php
                    foreach($factory_washmode as $k => $v)
                    {
                        
                @endphp

                <input type="hidden" name="fw_id[]"  value="{{$v['id']}}" lay-skin="primary" lay-verify="fw_id" >';

                @if ($v['qs_price'] >=0 )
                <div class="layui-form-item">
                    <label for="" class="layui-form-label">{{$washmode['qs'][0]['name']}}</label>
                    <div class="layui-input-inline" >
                        <input type="text" name="qs_price1"  value="{{$washmodel_arra[0]['qs_price']??$v['qs_price']}}" title="价格" placeholder="价格" class="layui-input" lay-verify="qs_price1" >
                    </div>
                    <div class="layui-inline layui-word-aux">几分钱</div>
                </div>
                @endif 

                @if ($v['pm_price'] >=0 )
                <div class="layui-form-item">
                    <label for="" class="layui-form-label">{{$washmode['pm'][0]['name']}}</label>
                    <div class="layui-input-inline" >
                        <input type="text" name="pm_price1"  value="{{$washmodel_arra[0]['pm_price']??$v['pm_price']}}" title="价格" placeholder="价格" class="layui-input" lay-verify="pm_price1" >
                    </div>
                    <div class="layui-inline layui-word-aux">几分钱</div>
                </div>
                @endif 

                @if ($v['xc_price'] >=0 )
                <div class="layui-form-item">
                    <label for="" class="layui-form-label">{{$washmode['xc'][0]['name']}}</label>
                    <div class="layui-input-inline" >
                        <input type="text" name="xc_price1"  value="{{$washmodel_arra[0]['xc_price']??$v['xc_price']}}" title="价格" placeholder="价格" class="layui-input" lay-verify="xc_price1" >
                    </div>
                    <div class="layui-inline layui-word-aux">几分钱</div>
                </div>
                @endif 

                @if ($v['xs_price'] >=0 )
                <div class="layui-form-item">
                    <label for="" class="layui-form-label">{{$washmode['xs'][0]['name']}}</label>
                    <div class="layui-input-inline" >
                        <input type="text" name="xs_price1"  value="{{$washmodel_arra[0]['xs_price']??$v['xs_price']}}" title="价格" placeholder="价格" class="layui-input" lay-verify="xs_price1" >
                    </div>
                    <div class="layui-inline layui-word-aux">几分钱</div>
                </div>
                @endif 

                @if ($v['xd_price'] >=0 )
                <div class="layui-form-item">
                    <label for="" class="layui-form-label">{{$washmode['xd'][0]['name']}}</label>
                    <div class="layui-input-inline" >
                        <input type="text" name="xd_price1"  value="{{$washmodel_arra[0]['xd_price']??$v['xd_price']}}" title="价格" placeholder="价格" class="layui-input" lay-verify="xd_price1" >
                    </div>
                    <div class="layui-inline layui-word-aux">几分钱</div>
                </div>
                @endif


                @php
                    }
                @endphp
                @endif
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <button type="submit" class="layui-btn" lay-submit="" lay-filter="formDemo">确 认</button>
                    </div>
                </div>

            </form>
        </div>
    </div>
@endsection

@section('script')
<script>
layui.use(['upload', 'layer', 'form', 'laydate'], function () {
    var $ = layui.jquery
        , upload = layui.upload
        , form = layui.form
        , layer = layui.layer
        , laydate = layui.laydate;

    laydate.render({
        elem: '#install_time' //指定元素
        ,type:'datetime'
    });

    function GetTitleKey(kname){
        title = '';
        switch(kname){
        case 'qs':
            title = '清水单价';
            break;
        case 'pm':
            title = '泡沫单价';
            break;
        case 'xc':
            title = '吸尘单价';
            break;
        case 'xs':
            title = '洗手单价';
            break;
        case 'xd':
            title = '消毒单价';
            break;
        }
        return title;
    }

    function GetWashModeHtml(kname, washing_type,  minutes, price, modeid){
        var title = GetTitleKey(kname);
        str = '';
        str += '<div class="layui-form-item">';
        //str +='<label for="" class="layui-form-label"></label>'; 

        //str +='<div class="layui-input-inline" style="width: 100px;">';
        str +='<label for="" class="layui-form-label">'+title+'</label>';
        //str +='</div>';

        //str +='<div class="layui-input-inline">';
        //str +='<input type="text" name="'+kname+'_second'+washing_type+'" id="'+kname+'_second'+washing_type+'"  value="'+minutes+'" title="'+title+'时间" placeholder="'+kname+'时间，分钟" class="layui-input" lay-verify="'+kname+'_second" >';
        //str +='</div>';
        str +='<div class="layui-input-inline">';
        str +='<input type="text" name="'+kname+'_price'+washing_type+'" id="'+kname+'_price'+washing_type+'" value="'+price+'" title="价格" placeholder="金额，分" class="layui-input" lay-verify="'+kname+'_price'+washing_type+'" >';
        str +='</div>';
        str += '<div class="layui-inline layui-word-aux">几分钱</div>';
        str +='</div>';

        return str;
    }

    $("#device_no").blur(function (){
        var device_no = $(this).val();
        if (device_no != ""){
            $.ajax({
                url: '/admin/site/getdeviceinfo',
                method: 'get',
                data: {
                    device_no: device_no
                },
                success: function (res) {
                    var data = res.data;
                    $("#factory_id").val(data.factory.id);
                    $("#factory_name").val(data.factory.name);
                    $("#device_no").val(data.device_no);
                    $("#device_model").val(data.device_model);
                    $("#device_id").val(data.id);

                    if (data.factory.washmode instanceof Array) {
                        var str = '';
                        var mode_ids = [];
                        for(i =0; i < data.factory.washmode.length; i++)
                        {
                            //console.log(data.factory.washmode[i]);
                            var labname = '',checkstr = '';
                            if (i == 0){
                                labname = '洗车模式';
                                checkstr = 'checked';
                            }
                            var modename = data.factory.washmode[i].name;
                            var modeid   = data.factory.washmode[i].id;
                            var price    = "";
                            var minutes    = "";
                            var washing_type = data.factory.washmode[i].washing_type;
                            //var price    = data.factory.washmode[i].price;
                            //var minutes  = data.factory.washmode[i].minutes;
                            mode_ids.push(modeid);

                            str += '<div class="layui-form-item">';
                            //str +='<label for="" class="layui-form-label">'+labname+'</label>';
                            //str +='<div class="layui-input-block">';
                            //str +='<div class="layui-input-inline" style="width: 120px;">';
                            str +='<input type="checkbox" name="fw_id[]"  value="'+modeid+'" title="'+modename+'" lay-skin="primary" lay-verify="fw_id" '+checkstr+'>';
                            str +='<input type="hidden" name="washing_type'+modeid+'" id="washing_type'+washing_type+'" value="'+washing_type+'" class="layui-input" lay-verify="washing_type'+washing_type+'" >';
                            //str +='</div>';
                            //str +='</div>';
                            str +='</div>';

                            //清水
                            if (data.factory.washmode[i].qs_price >= 0){
                                var te_minutes = data.factory.washmode[i].qs_second;
                                var te_price = data.factory.washmode[i].qs_price;
                                var te_html = GetWashModeHtml('qs', washing_type, te_minutes, te_price, modeid);
                                str += te_html;
                            }
                            //泡沫
                            if (data.factory.washmode[i].pm_price >= 0){
                                var te_second = data.factory.washmode[i].pm__second;
                                var te_price = data.factory.washmode[i].pm_price;
                                var te_html = GetWashModeHtml('pm', washing_type, te_minutes, te_price, modeid);
                                str += te_html;
                            }
                            //吸尘
                            if (data.factory.washmode[i].xc_price >= 0){
                                var te_minutes = data.factory.washmode[i].xc_second;
                                var te_price = data.factory.washmode[i].xc_price;
                                var te_html = GetWashModeHtml('xc', washing_type, te_minutes, te_price, modeid);
                                str += te_html;
                            }
                            //洗手
                            if (data.factory.washmode[i].xs_price >= 0){
                                var te_minutes = data.factory.washmode[i].xs_second;
                                var te_price = data.factory.washmode[i].xs_price;
                                var te_html = GetWashModeHtml('xs', washing_type, te_minutes, te_price, modeid);
                                str += te_html;
                            }
                            //消毒
                            if (data.factory.washmode[i].xd_price >= 0){
                                var te_minutes = data.factory.washmode[i].xd_second;
                                var te_price = data.factory.washmode[i].xd_price;
                                var te_html = GetWashModeHtml('xd', washing_type, te_minutes, te_price, modeid);
                                str += te_html;
                            }
                        }

                        var mode_ids_str = "";
                        console.log(mode_ids);
                        if (mode_ids.length > 0){
                            mode_ids_str = mode_ids.join(",");
                        }
                        $("#mode_ids_str").val(mode_ids_str);
                        $("#xichemoshi").html(str);
                        layui.form.render();
                    }
                }
            });
        }
    });


    var verify = {
        device_no:function (value,item){
            if (value == ""){
                return '设备编号不能为空';
            }
        },
        factory_id:function (value,item){
            if (value == ""){
                return '机器厂商不能为空';
            }
        },
        device_model:function (value,item){
            if (value == ""){
                return '设备型号不能为空';
            }
        },
        install_time:function(value,item){
            if(value == ""){
                return '安装时间不能为空';
            };
        },
        fw_id:function (value,item){

            var fw_id = $('input[name="fw_id"]:checked').val();
            var price = $("#price_"+fw_id).val();
            var minutes = $("#minutes_"+fw_id).val();
            if (fw_id > 0 && minutes == ""){
                return '洗车时间必须大于0';
            }
            if (fw_id > 0 && price == ""){
                return '洗车花费必须大于0';
            }
        }
    };
    form.verify(verify);

    form.on('submit(formDemo)', function (data) {
        $('.layui-form').submit();
    });
})
</script>
@endsection
